import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import Hero from './hero';
import { HeroService } from './hero.service';

@Component({
  moduleId: module.id,
  selector: 'my-heroes',
  templateUrl: 'heroes.component.html',
  styleUrls: [ 'heroes.component.css' ],
})
export class HeroesComponent {
  heroes: Hero[]; // 初始化结构类型

  // 构造函数，依赖注入
  constructor(
    private router: Router,
    private heroService: HeroService
    ) { }

  // 异步请求数据
  getHeroes(): void {
    this.heroService.getHeroes()
    .then(heroes => this.heroes = heroes);
  }
  // 组件初始化时，调用方法
  ngOnInit(): void {
    this.getHeroes();
  }

  selectHero: Hero;
  // 英雄选择
  onSelect(hero: Hero): void {
    this.selectHero = hero;
  }
  // 点击按钮访问详情
  gotoDetail(): void {
    this.router.navigate(['/detail', this.selectHero.id]);
  }
}
